/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dirox.plugins.utils;

import com.atlassian.mail.Email;
import com.atlassian.mail.MailException;
import com.atlassian.mail.server.SMTPMailServer;
import com.atlassian.jira.component.ComponentAccessor;

public class MailHelper {
	
	private static MailHelper instance = null;
	private static SMTPMailServer server = null;

	private MailHelper() {
		server = ComponentAccessor.getMailServerManager().getDefaultSMTPMailServer();
	}

	private static MailHelper getInstance() {
		if (instance == null) {
			instance = new MailHelper();
		}

		return instance;
	}

	public static void setPrefix(String prefix) {
		if (getInstance().server != null) {
			getInstance().server.setPrefix(prefix);
		}
	}

	public static String getDefaultFrom() {
		if (getInstance().server != null) {
			return getInstance().server.getDefaultFrom();
		} else {
			return "";
		}
	}

	public static boolean send(Email email) {
		try	{
			if (getInstance().server != null) {
				getInstance().server.send(email);		
				return true;
			}
		} catch (MailException e) {
			// do nothing
        }

		return false;
	}

	public static boolean send(String from, String to, String subject, String body) {
		Email email = new Email(to);

		email.setFrom(from);
		email.setSubject(subject);
		email.setMimeType("text/html");
		email.setBody(body);

		return send(email);
	}

	public static boolean send(String to, String subject, String body) {
		return send(getDefaultFrom(), to, subject, body);
	}
}
